<?php
session_start();

if(!isset($_SESSION['id']))
header("Location: index.php"); //Renvoie sur l'index si pas de session.

include 'include/fctAux.inc.php';
include "include/DB.inc.php";

enTete("eAgenda - Accueil");

$id=$_SESSION['id'];
?>

<div class="haut">
	<?php logo(); include 'include/menu.inc.php';?> 
</div>

<div class="menuGauche">
	<?php include 'include/menuGauche.inc.php';?>
</div>

<div class="corps">
<?php 
if(isset($_GET['msg'])) $idmsg = $_GET['msg'];
//Affichage du message précedement selectioné (si un idmsg defini ET si un message correspond et que l'utilisateur en est recepteur ou envoyeur).
if(isset($idmsg) && count(DB::select("select * from agenda_messagemes where idmesm = $idmsg AND (iduserr = $id OR idusere = $id)")) == 1) {

	$msg = DB::select("select * from agenda_messagemes where idmesm = $idmsg");
	$date = $msg[0]->dateenvoie;
	$corps = $msg[0]->corps;
	
	if($msg[0]->iduserr == $id) { //Si l'user est recepteur
		$idE = $msg[0]->idusere;
		$descrip = DB::select("select prenomu, nomu from agenda_utilisateur where iduser = $idE");
		$nomE = $descrip[0]->nomu;
		$prenomE = $descrip[0]->prenomu;		
		$descrip = "Message reçu le $date par $prenomE $nomE :";
		DB::maj("UPDATE agenda_messagemes SET dejalu = true WHERE idmesm = $idmsg AND dejalu = false"); //Passe le msg en lu
	} else {
		$idR = $msg[0]->iduserr;
		$descrip = DB::select("select prenomu, nomu from agenda_utilisateur where iduser = $idR");
		$nomR = $descrip[0]->nomu;
		$prenomR = $descrip[0]->prenomu;
		$descrip = "Message envoyé le $date à $prenomR $nomR :";
	}
	
	echo "<div class='messagerie'>\n";
	echo "<p>$descrip</p>";
	echo "<div class='message'>".nl2br($corps)."</div>";
	echo "</div>\n";
}

// Recuperation de tous les messages envoyés et recus.
$msgRecu = DB::select("select * from agenda_messagemes, agenda_utilisateur where iduserr=$id and idusere=iduser ORDER BY dateenvoie DESC");
$msgEnvoye = DB::select("select * from agenda_messagemes, agenda_utilisateur where idusere=$id and iduserr=iduser ORDER BY dateenvoie DESC");

// Liste des msgs recus :
echo "<div class='messagerie'>\n";
	echo "<p>Messages reçus :</p>\n";
	
	echo "<div>\n";
		echo "<table>\n";
		$i=0;
		foreach ($msgRecu as $msg) {
			$idmsg = $msg->idmesm;
			$etat = $msg->dejalu;
			$date = $msg->dateenvoie;
			$nomE = $msg->nomu;
			$prenomE = $msg->prenomu;
			$corps = substr($msg->corps, 0, 10)."...";
			
			if($etat!="f")
				if($i%2==0)
					echo "<tr class='pair'> <th>Lu</th>";
				else 
					echo "<tr> <th>Lu</th>";
			else
					echo "<tr class='nonlu'><th>Non lu</th>";
			echo "<td>le $date</td><td><a href='messagerie.php?msg=$idmsg'>$corps</a></td><td>Envoyé par : $prenomE $nomE</td></tr>\n";
			$i++;
		}
		echo "</table>\n";
	echo "</div>\n";
echo "</div>\n";


//Liste des messages envoyés :
echo "<div class='messagerie'>\n";
	echo "<p>Messages envoyés :</p>";
	echo "<div>\n";
		echo "<table>";
		$i=0;
		foreach ($msgEnvoye as $msg) {
			$idmsg = $msg->idmesm;
			$date = $msg->dateenvoie;
			$nomR = $msg->nomu;
			$prenomR = $msg->prenomu;
			$corps = substr($msg->corps, 0, 10)."...";
			
			if($i%2==0)
				echo "<tr class='pair'>";
			else
				echo "<tr>";
			
			echo "<td>le $date</td><td><a href='messagerie.php?msg=$idmsg'>$corps</a></td><td>Envoyé à : $prenomR $nomR</td></tr>";
			$i++;
		}
		echo "</table>";
	echo "</div>\n";
echo "</div>\n";
?>
</div>
<?php pied(); ?>